package chart;

import java.awt.Color;
import java.awt.Paint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

import javax.imageio.ImageIO;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.jCharts.chartData.ChartDataException;
import org.jCharts.chartData.PieChartDataSet;
import org.jCharts.nonAxisChart.PieChart2D;
import org.jCharts.properties.ChartProperties;
import org.jCharts.properties.LegendProperties;
import org.jCharts.properties.PieChart2DProperties;
import org.jCharts.properties.PropertyException;
import org.xml.sax.SAXException;

import settings.MasterController;
import entity.Category;
import entity.Expense;
import entity.Income;

//@author U096819H
public class PieChartCategory {

	//@author U096819H
	public void generateChart(MasterController mc) throws ChartDataException,
			PropertyException, TransformerConfigurationException,
			InstantiationException, IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {

		ArrayList<Category> catList = mc.GetAllCategory();
		Map<String, Double> expList = new TreeMap();
		Map<String, Double> incList = new TreeMap();

		expList = mc.GetTotalExpenseByCategory();
		incList = mc.GetTotalIncomeByCategory();

		String[] labels = new String[catList.size()];
		double[] dataExp = new double[catList.size()];
		double[] dataInc = new double[catList.size()];
		
		double totalExp = mc.GetTotalExpense();
		double totalInc = mc.GetTotalIncome();

		for (int i = 0; i < catList.size(); i++) {
			dataExp[i] = (expList.get(catList.get(i).getName())/totalExp)*100d;
			dataInc[i] = (incList.get(catList.get(i).getName())/totalInc)*100d;
			labels[i] = catList.get(i).getName();
		}

		Random rand = new Random();
		float r, g, b;
		Paint[] paints = new Paint[catList.size()];
		for (int i = 0; i < catList.size(); i++) {
			r = rand.nextFloat();
			g = rand.nextFloat();
			b = rand.nextFloat();
			paints[i] = new Color(r, g, b);
		}

		PieChart2DProperties pieChart2DProperties = new PieChart2DProperties();
		PieChartDataSet pieChartDataSetExp = new PieChartDataSet("Expense",
				dataExp, labels, paints, pieChart2DProperties);
		PieChartDataSet pieChartDataSetInc = new PieChartDataSet("Income",
				dataInc, labels, paints, pieChart2DProperties);

		PieChart2D pieChart2DExp = new PieChart2D(pieChartDataSetExp,
				new LegendProperties(), new ChartProperties(), 218, 218);
		PieChart2D pieChart2DInc = new PieChart2D(pieChartDataSetInc,
				new LegendProperties(), new ChartProperties(), 218, 218);

		BufferedImage bufferedImage = new BufferedImage(218, 218,
				BufferedImage.TYPE_INT_RGB);
		pieChart2DExp.setGraphics2D(bufferedImage.createGraphics());
		pieChart2DExp.render();
		File outputfile = new File("graphs", "catExp.png");
		ImageIO.write(bufferedImage, "png", outputfile);
		// income
		pieChart2DInc.setGraphics2D(bufferedImage.createGraphics());
		pieChart2DInc.render();
		outputfile = new File("graphs", "catInc.png");
		ImageIO.write(bufferedImage, "png", outputfile);
	}

}
